package com.lening.sellergoods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lening.mapper.TbSpecificationMapper;
import com.lening.mapper.TbSpecificationOptionMapper;
import com.lening.pojo.*;
import com.lening.pojo.TbSpecificationExample.Criteria;
import com.lening.pojogroup.Specification;
import com.lening.sellergoods.service.SpecificationService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {

	@Autowired
	private TbSpecificationMapper specificationMapper;
	@Autowired
	private TbSpecificationOptionMapper pecificationOptionMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<TbSpecification> findAll() {
		return specificationMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbSpecification> page = (Page<TbSpecification>) specificationMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Specification specification) {
		TbSpecification tbSpecification = specification.getSpecification();
		specificationMapper.insert(tbSpecification);
		List<TbSpecificationOption> optionList = specification.getSpecificationOptionList();
		for (TbSpecificationOption option : optionList) {
			option.setSpecId(tbSpecification.getId());
			pecificationOptionMapper.insert(option);
		}
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Specification specification){
		//修改规格表
		TbSpecification tbSpecification = specification.getSpecification();

		specificationMapper.updateByPrimaryKey(tbSpecification);
		List<TbSpecificationOption> list = specification.getSpecificationOptionList();
		TbSpecificationOptionExample example = new TbSpecificationOptionExample();
		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
		criteria.andSpecIdEqualTo(tbSpecification.getId());
		pecificationOptionMapper.deleteByExample(example);
		for (TbSpecificationOption option : list) {
			option.setSpecId(tbSpecification.getId());
			pecificationOptionMapper.insert(option);
		}
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Specification findOne(Long id){

		//创建一个组合实体类
		Specification specification = new Specification();

		//把规格查询出来了
		TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);

		//查询这个规格对应的明细信息
		TbSpecificationOptionExample example = new TbSpecificationOptionExample();

		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();

		criteria.andSpecIdEqualTo(id);


		List<TbSpecificationOption> list = pecificationOptionMapper.selectByExample(example);
		specification.setSpecification(tbSpecification);
		specification.setSpecificationOptionList(list);
		return specification;
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			specificationMapper.deleteByPrimaryKey(id);
			//specificationMapper.deleteSpecificationOptionBySpid(id);
			TbSpecificationOptionExample example = new TbSpecificationOptionExample();
			TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
			criteria.andSpecIdEqualTo(id);
			pecificationOptionMapper.deleteByExample(example);
		}
	}
	
	
	@Override
	public PageResult findPage(TbSpecification specification, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbSpecificationExample example=new TbSpecificationExample();
		Criteria criteria = example.createCriteria();
		
		if(specification!=null){			
						if(specification.getSpecName()!=null && specification.getSpecName().length()>0){
				criteria.andSpecNameLike("%"+specification.getSpecName()+"%");
			}
	
		}
		
		Page<TbSpecification> page= (Page<TbSpecification>)specificationMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public List<Map> selectOptionList() {
		return specificationMapper.selectOptionList();
	}

	@Override
	public PageResult search(Integer pageNum, Integer pageSize, TbSpecification searchEntity) {
		PageHelper.startPage(pageNum,pageSize);

		TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
		Criteria criteria = tbSpecificationExample.createCriteria();

		if(searchEntity!=null){
			if (searchEntity.getSpecName()!=null && searchEntity.getSpecName().length()>=1){

				criteria.andSpecNameLike("%"+searchEntity.getSpecName()+"%");

			}
		}

		Page<TbSpecification> page = (Page<TbSpecification>) specificationMapper.selectByExample(tbSpecificationExample);

		return new PageResult(page.getTotal(), page.getResult());
	}

}
